<template>
    <div id="main" class="clearfix">
        <HomeSiderBar 
            @isCollapse="(data) => { isCollapse = data; }"
            @list="(data) => { list = data; }"
            v-bind:studentdetail="studentdetail"
            v-bind:userdetail="userdetail"
            >
        </HomeSiderBar>
        <TopBar :list="list"></TopBar>
        <div  v-if="path =='/DormitoryMange'" class="top">
            <el-col :span="4">
                <el-input v-model="studentdetail.name" :disabled="true">
                </el-input>
            </el-col>
            <el-col :span="4" style="float: left;width: 16.7%;margin-left: 15px;">
                <el-input v-model="studentdetail.majorGetDTO.academyEntity.name" :disabled="true">
                </el-input>
            </el-col>
            
            <el-col :span="4" style="float: left;width: 16.7%;margin-left: 15px;">
                <el-input v-model="serachbuilddormitory" placeholder="请输入寝室号" suffix-icon="el-icon-s-home"></el-input>
            </el-col>
            <el-col :span="2" style="float: left;margin-left: 15px;">
                <el-button style="width:80%" type="primary" plain @click="serach">搜索</el-button>
            </el-col>
            <el-col :span="2" style="float: left;">
                <el-button style="width:80%" type="success" plain @click="initDor">重制</el-button>
            </el-col>
            <el-col :span="3" style="float: left;font-weight: 700;">
                我的宿舍：{{ studentdetail.dormitoryGetTDO.number == "" ? "未选择宿舍" : studentdetail.dormitoryGetTDO.buildingEntity.bid+"#"+studentdetail.dormitoryGetTDO.number }}
            </el-col>
            <div style="width:0px"></div>
        </div>
        
        <router-view class="clearfix" 
            :isCollapse="isCollapse" 
            v-bind:studentdetail="studentdetail"
            v-bind:userdetail="userdetail"
            v-bind:collegedata="collegedata"
            v-bind:dormitory="dormitory"
            v-bind:active="studentdetail.userGetCheckTDO.state"
            @updateDormitory="updateDormitory"
            @updateInfo="updateInfo"
        />
    </div>
</template>

<script>
import TopBar from '@/components/TopBar.vue'
import HomeSiderBar from '../components/HomeSideBar'
import axios from 'axios';
import DormitoryMange from './DormitoryMange.vue';

export default {
    data(){
        return{
            list:[],
            isCollapse: true,
            path:"",
            studentdetail: {
            },
            userdetail: {
                uid:3,
                username: "Jerry",
                phone: "18071497860",
                role: 0,//0表示学生，1表示管理员
            },
            serachbuild: "",
            serachbuilddormitory: "",
            dormitory: [],
            collegedata:[
                { value: 1048, name: '未报到' },
                { value: 735, name: '已报道，未缴费' },
                { value: 580, name: '已报道，已缴费' },
                { value: 484, name: '总人数' }
            ],
            
        }
    },
    components:{
        HomeSiderBar,
        TopBar,
        DormitoryMange
    },
    methods:{
        //宿舍查询
        serach() {
            if (this.serachbuilddormitory == "") {
                this.$message.error('请输入内容');
            } else {
                var _this=this;
                // console.log(this.serachbuilddormitory);
                axios({
                    method: "get",
                    url: "api/dormitories/getDormitoryByNumber/" + this.serachbuilddormitory
                }).then(function (resp) {
                    _this.dormitory = resp.data.data;
                })
            }
        },
        //更新宿舍数据
        updateDormitory(val){
            val.forEach((item) => {
                item.buildingEntity={
                    bid: item.bid
                }
                item.personNumber=item.personNum
            });
            this.dormitory=val;
            // console.log(this.dormitory);
        },
        initDor(){
            var _this=this;
            //获取所有宿舍信息
            axios({
                method: "get",
                url: "api/dormitories/getAllDormitory?pageSize=1000&pageNum=1"
            }).then(function (resp) {
                _this.dormitory = resp.data.data.data;
            })
        },
        updateInfo(){
            var _this=this;
            axios({
                method: "get",
                url: "api/users/getInfoByUid/" + _this.userdetail.uid,
            }).then(function (resp) {
                _this.studentdetail = resp.data.data;
                console.log(resp.data.data);
            })
        }
    },
    watch:{
        $route(to,from){
            // console.log(to.path);
            this.path=to.path;
        }
    },
    mounted(){

        // console.log(localStorage.getItem("userInfo"))
        this.userdetail= JSON.parse(localStorage.getItem("userInfo"));
        // console.log(temp);
        // console.log(temp.role);
        // console.log(this.userdetail.role);
        if (this.userdetail.role == 0) {//学生端默认路由
            this.$router.push("/ArriveMange")
        } else if (this.userdetail.role == 1){//管理员端默认路由
            this.$router.push("/ArriveInformation")
        }

        var _this=this;
        // 根据登录信息获取info信息
        axios({
            method: "get",
            url: "api/users/getInfoById/"+_this.userdetail.uid,
        }).then(function (resp) {
            _this.studentdetail=resp.data.data;
            console.log(resp.data.data);
        })
       
        this.initDor();
    }
    
    
    
}
</script>
<style scoped>
#main{
    background-color: #f6f6f6;
}
.top {
    text-align: initial;
    width: 100%;
    height: 50px;
    line-height: 50px;
    background-color: #ffffff;
    border-bottom: 0.7px solid rgba(0, 0, 0, .2);
}
.clearfix::after {
    content: "";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}

.clearfix {
    /* IE6、7 专有 */
    *zoom: 1;
}
</style>